package pdm.traj.update;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import pdm.traj.conf.TrajConf;
import pdm.traj.entity.TrackPoint2;
import pdm.traj.service.KeyService;
import redis.clients.jedis.Jedis;

public class UpdateTraj implements Runnable{
	public static final String TAG = "UpdateTraj";
	private String ip;
	private int port;
	private Jedis cli;
	private KeyService keyService;
	private boolean initReady = false;
	
//	private ThreadLocal<List<TrackPoint2>> oldList;
//	private ThreadLocal<List<TrackPoint2>> curList; 
	private List<TrackPoint2> oldList;
	private List<TrackPoint2> curList = new ArrayList<TrackPoint2>(); 
	
	public UpdateTraj(String ip, int port) {
		this.ip = ip;
		this.port = port;
	}
	
	@Override
	public void run() {
		init();
	}
	
	public void init() {
//		System.out.println(TAG + ": init: ip = " + ip + "\tport = " + port);
		cli = new Jedis(ip, port);
		
//		curList = new ArrayList<TrackPoint2>();
		Timer t = new Timer();
		int cycle = Integer.parseInt(TrajConf.getInstance().getProperty(TrajConf.UPDATE_TRAJ_CYCLE));
		t.scheduleAtFixedRate(new UpdateThread(), cycle, cycle);
		initReady = true;
	}
	
	class UpdateThread extends TimerTask {
		public static final String TAG = "UpdateTraj.UpdateThread";
		@Override
		public void run() {
//			synchronized(UpdateTraj.class) {
				oldList = curList;
				curList = new ArrayList<TrackPoint2>();
//			}
//			System.out.println(TAG + "ip: " + ip  + "\t: oldListLength: " + oldList.size());
			
//			synchronized(this) {
				try {
			if (oldList.size() > 0) {
				cli.connect();
				for (TrackPoint2 tp : oldList) {
					String key = keyService.getKeyByTP(tp);
					cli.append(key, tp.getEntryValue() + ";");
				}
				cli.disconnect();
			}
				} catch (Exception e) {
//					e.printStackTrace();
				}
//			}
		}
	}
	
//	class UpdateThread extends Thread {
//		TrackPoint2 tp;
//		public void run() {
//			String key = keyService.getKeyByTP(tp);
//			if (!cli.isConnected()) {
//				cli.connect();
//			}
//			cli.append(key, tp.getEntryValue() + ";");		
////			cli.disconnect();
//		}
//		public TrackPoint2 getTp() {
//			return tp;
//		}
//		public void setTp(TrackPoint2 tp) {
//			this.tp = tp;
//		}
//		
//	}
	
	public int update(TrackPoint2 tp) {
//		synchronized(this) {
//		if (initReady) {
			curList.add(tp);
//		} else {
//			System.err.println(TAG + ": update initReady is false");
//		}
//		}
		return 1;
	}
	
//	public int update(TrackPoint2 tp) {
//		UpdateThread ut = new UpdateThread();
//		ut.setTp(tp);
//		ut.start();
////		Runnable r = new Runnable() {
////			public void run() {
////				String key = keyService.getKeyByTP(tp);
////				cli.connect();
////				cli.append(key, tp.getEntryValue() + ";");		
////				cli.disconnect();				
////			}
////		}
////		r.run();
//		return 1;
//	}
	
//	public int update(TrackPoint2 tp, String key) {
//		cli.connect();
//		cli.append(key, tp.getEntryValue() + ";");		
////		cli.lpush(key, tp.getEntryValue());
////		if (cli.exists(key)) {
////			// append
////			cli.append(key, tp.getEntryValue());
////		} else {
////			// new key arrived
////			cli.set(key, tp.getEntryValue());
////		}
//		cli.disconnect();
//		return 1;
//	}
	public int getCurListSize() {
		return curList.size();
	}
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}
	public KeyService getKeyService() {
		return keyService;
	}
	public void setKeyService(KeyService keyService) {
		this.keyService = keyService;
	}




	
}
